<?php print "<?php"; ?>


namespace Hunter\<?php print $module; ?>\Controller;

use Zend\Diactoros\ServerRequest;
use Psr\Http\Message\UploadedFileInterface;
use Zend\Diactoros\Response\JsonResponse;

/**
 * Class <?php print $module; ?>.
 *
 * @package Hunter\<?php print $module; ?>\Controller
 */
class <?php print ucfirst($module); ?>Controller {
  /**
   * <?php print $module; ?>_list.
   *
   * @return string
   *   Return <?php print $module; ?>_list string.
   */
  public function <?php print $module; ?>_list() {
    $<?php print $module; ?>_list = get_all_<?php print $module; ?>();

    return view('/admin/<?php print $module; ?>-list.html', array('<?php print $module; ?>s' => $<?php print $module; ?>_list));
  }

  /**
   * <?php print $module; ?>_add.
   *
   * @return string
   *   Return <?php print $module; ?>_add string.
   */
  public function <?php print $module; ?>_add(ServerRequest $request) {
    if ($parms = $request->getParsedBody()) {
      $user = session()->get('admin');
      $title = $parms['title'];
      <?php foreach($fields as $field): ?>
$<?php print $field['name']; ?> = $parms['<?php print $field['name']; ?>'];
      <?php endforeach; ?>

      $<?php print substr($module, 0, 1 ); ?>id = db_insert('<?php print $module; ?>')
        ->fields(array(
          'title' => $title,
          <?php foreach($fields as $field): ?>
'<?php print $field['name']; ?>' => $<?php print $field['name']; ?>,
          <?php endforeach; ?>
          'uid' => $user->uid,
          'created' => time(),
          'updated' => time(),
        ))
        ->execute();

      return $<?php print substr($module, 0, 1 ); ?>id;
    }

    return view('/admin/<?php print $module; ?>-add.html');
  }

  /**
   * <?php print $module; ?>_edit.
   *
   * @return string
   *   Return <?php print $module; ?>_edit string.
   */
  public function <?php print $module; ?>_edit($<?php print substr($module, 0, 1 ); ?>id) {
      $<?php print $module; ?> = get_<?php print $module; ?>_byid($<?php print substr($module, 0, 1 ); ?>id);

      return view('/admin/<?php print $module; ?>-edit.html', array('<?php print $module; ?>' => $<?php print $module; ?>, '<?php print substr($module, 0, 1 ); ?>id' => $<?php print substr($module, 0, 1 ); ?>id));
  }

  /**
   * <?php print $module; ?>_update.
   *
   * @return string
   *   Return <?php print $module; ?>_update string.
   */
  public function <?php print $module; ?>_update(ServerRequest $request) {
    if ($parms = $request->getParsedBody()) {
      $<?php print substr($module, 0, 1 ); ?>id = $parms['<?php print substr($module, 0, 1 ); ?>id'];
      $user = session()->get('admin');
      $title = $parms['title'];
      <?php foreach($fields as $field): ?>
$<?php print $field['name']; ?> = $parms['<?php print $field['name']; ?>'];
      <?php endforeach; ?>

       db_update('<?php print $module; ?>')
         ->fields(array(
           'title' => $title,
           <?php foreach($fields as $field): ?>
'<?php print $field['name']; ?>' => $<?php print $field['name']; ?>,
           <?php endforeach; ?>
           'uid' => $user->uid,
           'updated' => time(),
         ))
         ->condition('<?php print substr($module, 0, 1 ); ?>id', $<?php print substr($module, 0, 1 ); ?>id)
         ->execute();

       return true;
     }
     return false;
  }

  /**
   * <?php print $module; ?>_del.
   *
   * @return string
   *   Return <?php print $module; ?>_del string.
   */
  public function <?php print $module; ?>_del($<?php print substr($module, 0, 1 ); ?>id) {
    $result = db_delete('<?php print $module; ?>')
            ->condition('<?php print substr($module, 0, 1 ); ?>id', $<?php print substr($module, 0, 1 ); ?>id)
            ->execute();

    if ($result) {
      return true;
    }

    return false;
  }

  /**
   * get <?php print $module; ?> list
   */
  public function get_<?php print $module; ?>_list() {
      $<?php print $module; ?>_list = db_select('<?php print $module; ?>', '<?php print substr($module, 0, 1 ); ?>')
        ->fields('<?php print substr($module, 0, 1 ); ?>')
        ->execute()
        ->fetchAllAssoc('<?php print substr($module, 0, 1 ); ?>id');

      return $<?php print $module; ?>_list;
  }

  <?php foreach ($fields as $field): ?>
    <?php if($field['html_type'] == 'file' || $field['html_type'] == 'image' || $field['html_type'] == 'textarea'): ?>

  /**
   * <?php print $module; ?>_<?php print $field['name']; ?>_upload.
   *
   * @return string
   *   Return <?php print $module; ?>_<?php print $field['name']; ?>_upload string.
   */
  public function <?php print $module; ?>_<?php print $field['name']; ?>_upload(ServerRequest $request) {
    $uploadedFiles = $request->getUploadedFiles();
    if (!empty($uploadedFiles)) {
      $files = array();
      foreach ($uploadedFiles as $key => $value) {

        if(empty(strstr($value->getClientMediaType(), 'image'))){
          return new JsonResponse(array('filename' => false));
        }

        if ($value instanceof UploadedFileInterface) {
          $url = 'sites/upload/<?php print $module; ?>/<?php print $field['name']; ?>/'. $value->getClientFilename();
          if (!is_dir(dirname($url))){
            mkdir(dirname($url), 0755, true);
          }
          $value->moveTo($url);
          return new JsonResponse(array('filename' => base_path().$url, 'ext' => $value->getClientMediaType()));
        }
      }
    }

    return new JsonResponse(array('filename' => false));
  }
<?php endif; ?>
<?php endforeach; ?>

}
